﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Security.Cryptography;
using imichaelmiers.wp7.security.cryptography.PasswordProtectedStorage;

namespace imichaelmiers.wp7.security.cryptography.PasswordProtectedStorage.Demo
{
    public partial class MainPage : PhoneApplicationPage
    {        
        public class model : DependencyObject
        {

            public static readonly DependencyProperty StatusProperty = 
                DependencyProperty.Register("Status", typeof(string), typeof(model),null);
            public string Status
            {
                get { return (string)GetValue(StatusProperty); }
                set { SetValue(StatusProperty, value); }
            }
            public static readonly DependencyProperty PasswordProperty =
                DependencyProperty.Register("Password", typeof(string), typeof(model), null);

            public string Password
            {
                get { return (string)GetValue(PasswordProperty); }
                set { SetValue(PasswordProperty, value); }
            }
            public static readonly DependencyProperty TagProperty =
                DependencyProperty.Register("Tag", typeof(string), typeof(model), null);
            public string Tag
            {
                get { return (string)GetValue(TagProperty); }
                set { SetValue(TagProperty, value); } 
            }

            public static readonly DependencyProperty ValueProperty =
                DependencyProperty.Register("Value", typeof(string), typeof(model), null);
            public string Value
            {
                get { return (string)GetValue(ValueProperty); }
                set { SetValue(ValueProperty, value); }
            }
            public void clear(){
                Status = "";
                Password = "";
                Tag = "";
                Value = "";
            }
            public model()
            {
                Status = "Input a Tag,a Value, and a Password to protect them with";
            }
            public void Add()
            {
                try
                {
                    PasswordProtectedStore.Store.Add(Password, Tag, Value);
                    this.Status = "Value added to store. Press get to rerive";
                }
                catch (ArgumentException e)
                {
                    this.Status = String.Format("error {0}", e.Message);
                }
             
            }
            public void Remove()
            {
                PasswordProtectedStore.Store.Remove(Tag);
                this.Status = "Data deleted ";
            }
            public void Get()
            {
                try
                {
                    string data = (string)PasswordProtectedStore.Store.Get(Password, Tag);
                    this.Status = String.Format("Retrived {0}", data);
                }
                catch (KeyNotFoundException)
                {
                    this.Status = String.Format("Not data stored under Tag {0}.", Tag);
                }
                catch (CryptographicException e)
                {
                    this.Status = String.Format("error {0}", e.Message);
                }
            }

        }

        public model modl;
        // Constructor
        public MainPage()
        {
              modl = new model();
            InitializeComponent();
            DataContext = modl;
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            modl.Add();
        }

        private void Remove_Click(object sender, RoutedEventArgs e)
        {
            modl.Remove();
        }

        private void Get_Click(object sender, RoutedEventArgs e)
        {
            modl.Get();
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            PasswordProtectedStore.Store.Save();
        }

    }
}